<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText" style="margin-bottom: 5px"></el-input>
    <el-tree :data="categories" :props="defaultProps" node-key="catId" ref="menuTree" @node-click="nodeclick"
      :filter-node-method="filterNode" :highlight-current="true"></el-tree>
  </div>
</template>

<script>
export default {
  data () {
    return {
      filterText: '',
      categories: [],
      expandedKey: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    };
  },
  //监控data中的数据变化
  watch: {
    filterText (val) {
      this.$refs.menuTree.filter(val)
    }
  },
  //方法集合
  methods: {
    //树节点过滤
    filterNode (value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    getCategories () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({ data }) => {
        this.categories = data.data
      });
    },
    nodeclick (data, node, component) {
      // 向父组件发送事件
      this.$emit('tree-node-click', data, node, component)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    this.getCategories()
  }
}
</script>
<style scoped>
</style>